<?php
namespace app\admin\controller;


use think\facade\View;
use think\Model;
use think\Request;

class Orders extends Base
{
    protected $ModelPath = 'Orders';
    protected $model = null;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->con = "Orders";
        $this->title = "普通订单";

        $module = $this->ModelPath;
        $this->model = new \app\admin\model\Orders();

        View::assign(array(
            'module' => $module,
            'title' => $this->title,
            'con' => $this->con,
            'pageSize' => $this->pageSize
        ));
    }

    public function index()
    {
        if(request()->isPost()){
            $params = request()->param();
            $map = [];
            $map[] = ['status', '=', 'payed'];

            if(!empty($params['ship_status'])){
                $map[] = ['ship_status', '=', $params['ship_status']];
            }
            if(!empty($params['id'])){
                $map[] = ['id', '=', $params['id']];
            }
            if(!empty($params['user_id'])){
                $map[] = ['user_id', '=', $params['user_id']];
            }

            $total = $this->model
                ->where($map)
                ->count();

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $this->model::with(['ordersGoods'])
                 ->where($map)
                // ->where('rec',1)

                ->order('id DESC')
                ->append(['pay_type_format', 'total_amount_format', 'address_format', 'ship_format', 'sku_name'])
                ->limit($p, $pageSize)
                ->select();
// var_dump($list);die;
            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            $ship_status = request()->param('ship_status', 'pending');
            $view_key = $ship_status != 'pending' ? 'index2' : 'index';
            View::assign(array(
                'ship_status'        =>    $ship_status,
            ));
            return View::fetch($view_key);
        }
    }

    public function add(){
        if(request()->isPost()){
            $code = input('code');
            $codeArr = explode(',', $code);
            $sum = $codeArr[0] + $codeArr[1] + $codeArr[2] + $codeArr[3];
            $num = input('num');
            $add = [
                'time'         =>  input('time'),
                'num'     =>  $num,
                'code'     =>  $code,
                'sum'     =>  $sum,
                'status'     =>  1,
            ];

//            var_dump($add);die;

            $this->M->data($add);
            $result = $this->M->save();
            if ($result !== false) {
                (new \app\model\Num())->insert([
                    'name'=>(int)$num + 1,
                    'type'=>1,
                ]);
                $arr['status'] = 1;
                $arr['msg'] = '添加成功';
            } else {
                $arr['status'] = 0;
                $arr['msg'] = '添加失败';
            }
            return $arr;
        }else{
//            $numArr = Db::name('num')
//                ->where('type', 1)
//                ->order('id DESC')->select();
//            View::assign(array(
//                'numArr'        =>    $numArr,
//            ));
            return View::fetch();
        }
    }

    public function delivery(){
        if(request()->isPost()){
            $ship_data = request()->param('ship_data', 0);
            if($ship_data == 0)
            {
                $arr['status'] = 0;
                $arr['msg'] = '发货失败';
            }else{
                $params = request()->param();
                $update = $params;
                $update['ship_status'] = 'delivery';
                $save = $this->model->update($update);
                if ($save) {
                    $arr['status'] = 1;
                    $arr['msg'] = '发货成功';
                } else {
                    $arr['status'] = 0;
                    $arr['msg'] = '发货失败';
                }
            }

            return $arr;
        }else{
            $id = input('id');
            $info = $this->model->where('id', $id)->find();
            View::assign(array(
                'info'        =>    $info,
            ));
            return View::fetch();
        }
    }

    public function edit(){
        if(request()->isPost()){
            $params = request()->param();
            $update = $params;

            $save = $this->model->update($update);
            if ($save) {
                $arr['status'] = 1;
                $arr['msg'] = '修改成功';
            } else {
                $arr['status'] = 0;
                $arr['msg'] = '修改失败';
            }
            return $arr;
        }else{
            $id = input('id');
            $info = $this->model->where('id', $id)->find();
            View::assign(array(
                'info'        =>    $info,
            ));
            return View::fetch();
        }
    }
}